package com.fr.report.script.core.parser;

import com.fr.base.ArrayUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.Function;
import com.fr.report.script.Primitive;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FunctionCall extends Atom
{
  private String name;
  private ConditionalOrExpression[] arguments;

  FunctionCall(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression)
  {
    this.name = paramString;
    this.arguments = paramArrayOfConditionalOrExpression;
  }

  public Object eval(Calculator paramCalculator)
    throws UtilEvalError
  {
    if ("if".equalsIgnoreCase(this.name))
      return ifFunction(paramCalculator);
    Function localFunction = paramCalculator.resolveMethod(this.name);
    if (localFunction == null)
      throw new InterpreterError("no function found: " + this.name);
    localFunction.setCalculator(paramCalculator);
    Object[] arrayOfObject = new Object[this.arguments.length];
    if (this.arguments != null)
      arrayOfObject = localFunction.processArguments(this.arguments);
    return localFunction.run(arrayOfObject);
  }

  private Object ifFunction(Calculator paramCalculator)
    throws UtilEvalError
  {
    if (ArrayUtils.getLength(this.arguments) < 3)
      return Primitive.ERROR_NAME;
    if (ComparatorUtils.equals(this.arguments[0].eval(paramCalculator), Boolean.FALSE))
      return this.arguments[2].eval(paramCalculator);
    return this.arguments[1].eval(paramCalculator);
  }

  public void traversal4Tiny(TinyHunter paramTinyHunter)
  {
    for (int i = 0; i < this.arguments.length; ++i)
      if (this.arguments[i] != null)
        this.arguments[i].traversal4Tiny(paramTinyHunter);
  }

  public String exString(Calculator paramCalculator)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(this.name).append("(");
    for (int i = 0; i < this.arguments.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(", ");
      if (this.arguments[i] != null)
        localStringBuffer.append(this.arguments[i].exString(paramCalculator));
    }
    localStringBuffer.append(")");
    return localStringBuffer.toString();
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(this.name).append("(");
    for (int i = 0; i < this.arguments.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(", ");
      if (this.arguments[i] != null)
        localStringBuffer.append(this.arguments[i]);
    }
    localStringBuffer.append(")");
    return localStringBuffer.toString();
  }

  public String getExpression(int paramInt1, int paramInt2, int paramInt3, int paramInt4, boolean paramBoolean)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(this.name).append("(");
    for (int i = 0; i < this.arguments.length; ++i)
    {
      if (i > 0)
        localStringBuffer.append(", ");
      if (this.arguments[i] != null)
        localStringBuffer.append(this.arguments[i].getExpression(paramInt1, paramInt2, paramInt3, paramInt4, paramBoolean));
    }
    localStringBuffer.append(")");
    return localStringBuffer.toString();
  }

  public String[] parserParameter()
  {
    ArrayList localArrayList = new ArrayList();
    int i = 0;
    int j = this.arguments.length;
    while (i < j)
    {
      localArrayList.addAll(Arrays.asList(this.arguments[i].parserParameter()));
      ++i;
    }
    return ((String[])localArrayList.toArray(new String[localArrayList.size()]));
  }
}